﻿package Levels 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.geom.Vector3D;
	import flash.ui.Keyboard;
	import ScreensManager.ScreenManager
	import ScreensManager.ProjectScreen.Screens
	import flash.media.SoundChannel;
	
	
	/**
	 * ...
	 * @author Thiago Gomes da Silva
	 */
	public class Level extends MovieClip 
	{
		private var main:Main;
		private var player:Player;
		private var background:Background;
		private var pontuacao:TextField;
		private var gameOver:TextField;
		private var enemy:Enemy;
		private var frames:int;
		private var pause:Boolean;
		private var enemyManager:EnemyManager;
		private var hitSound:Hit_Sound;
		private var soundChannel:SoundChannel;
		private var audioLastPosition:Number = 0;
		private var backgroundMusic:Background_Music;
		private var gameOverSound:Game_Over_Sound;
		private static var point:int;
		
		
		public function Level() 
		{
			
		}
		
		public function setLevel(main:Main)
		{
			//Player Attributes
			player = new Player(new Vector3D(10,-10,0),main,new Vector3D(0,1,0));
			this.player.x = main.stage.stageWidth / 2 - player.width / 2;
			this.player.y = main.stage.stageHeight - player.height;
			
			//Sounds
			soundChannel = new SoundChannel();
			hitSound = new Hit_Sound();
			backgroundMusic = new Background_Music();
			gameOverSound = new Game_Over_Sound();
			
			//Score Text Configuration
			this.pontuacao = new TextField();
			point = 125;
			this.pontuacao.defaultTextFormat = makeTextFormat(35, 0xFFFF00, "Brush Script MT")
			this.pontuacao.x = 5;
			this.pontuacao.y = 10;
			this.pontuacao.text = "" + point;
			this.addChild(pontuacao)
			
			//Game Over Text Configuration
			this.gameOver = new TextField();
			this.gameOver.defaultTextFormat = makeTextFormat(50, 0xFFFFFF, "Arial")
			this.gameOver.width = 500;
			this.gameOver.x = Main.getInstance().stage.stageWidth / 2 - this.gameOver.width / 3.5;
			this.gameOver.y = Main.getInstance().stage.stageHeight / 2 - gameOver.height / 2;
			this.gameOver.text = "GAME OVER";
			
			//Enemy Manager Instantiation			
			enemyManager = new EnemyManager(this);
			
			
			
			//Other Instantiations
			this.addChild(player)
			this.main = main;
			loadLevel(main);
			playSound();
		}
		
		//Score to Score Screen Function
		public static function getPoint():int
		{
			return point;
		}
		
		//Text Format Configuration
		private function makeTextFormat(size:int, color:uint, font:String):TextFormat
		{
			var format:TextFormat = new TextFormat();
			format.color = color;
			format.size = size;
			if(font != "")
				format.font = font;
			
			return format;
		}
		
		//Load Level - Background Motion Settings
		public function loadLevel(main:Main)
		{
			for (var k:int = 0; k < this.numChildren; k++)
			{
				var mc:MovieClip = this.getChildAt(k) as MovieClip;
				
				if (mc is Background)
				{
					this.background = mc as Background;
					this.background.setBackground(player, 10, main);
					this.background.y = 400 - this.background.height;
					
				}
			
			}
			
		}
		
		//SOUND
		
		 // Sound Loop Function
		public function loop(e:Event)
		{
			soundChannel.removeEventListener(Event.SOUND_COMPLETE, loop);
			soundChannel = backgroundMusic.play();
			soundChannel.addEventListener(Event.SOUND_COMPLETE, loop);
		}
		 // Sound Play Function
		public function playSound()
		{
			if (audioLastPosition >= 0)
				{
					soundChannel = backgroundMusic.play(audioLastPosition);
					soundChannel.addEventListener(Event.SOUND_COMPLETE, loop);
				}
		}
		
		//PAUSE
		
		public function isPaused():Boolean
		{
			return pause;
		}
		
		//Pause Menu Settings
		public function pauseButton()
		{	
			pause = true;
			ScreenManager.overwriteScreen(Screens.PAUSE_SCREEN);
			audioLastPosition = soundChannel.position;
			soundChannel.stop();
			soundChannel.removeEventListener(Event.SOUND_COMPLETE, loop);
		}
		
		public function resumeLevel()
		{
			pause = false;
			ScreenManager.deleteOverwrittenScreen();
			playSound();
		}
		
		public function finishMe()
		{
			soundChannel.stop();
			soundChannel = null;
			pause = false;
			audioLastPosition = 0;
			
		}
		
		//private function isFinishLevel():Boolean
		//{
			//if ((this.background.y - this.player.y) <= 0)
		//}
		
		//Level Update
		public function update()
		{
			if (pause)
			{
				return false;
			}
			else
			{
				player.update();
				enemyManager.update();
				
				for (var e:int = 0; e < enemyManager.getEnemies().length; e++)
				{
					//Enemy Countdown (24 = 1 per second)
					enemyManager.setEnemyCount(48);
					
					//Enemy Collision Configuration
					if (player.hitTestObject(enemyManager.getEnemies()[e]))
					{
						hitSound.play();
						
						(enemyManager.getEnemies()[e] as Enemy).disableMovement();
						
						this.removeChild(enemyManager.getEnemies()[e]);
						enemyManager.getEnemies().splice(e,1);
						
						//enemyManager.getEnemies()[e].disableMoviment();
						player.hasHitEnemy();
						point -= 5;
						this.pontuacao.text = point.toString();
					}
				}
				
				//Frame Countdown - Time
				if(frames >= 24)
				{
					frames = 0;
					point -= 1;
					this.pontuacao.text = point.toString();
				}
				else
				{
					frames++;
				}
				background.update();
			}
			
			//Finish Level
			if (this.background.y >= 0)
			{
				pause = true;
				soundChannel.stop();
				ScreenManager.setCurrentScreen(Screens.SCORE_SCREEN);
				
			}
			//Game Over
			if (point <= 0)
			{
				pause = true;
				soundChannel.stop();
				gameOverSound.play();
				this.addChild(gameOver)
			}
			
		}
		
	}

}